package com.java.test.algorithm;

import java.util.HashMap;
import java.util.Map;

/**
 * @author TonyOne
 * @version 1.0
 * @desc  给定一个乱序数组和一个目标数字 找到和为这个数字的两个数字 时间复杂度是多少
 * Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
 * @date 2018/10/17 11:59
 * @company
 */
public class ArrayIndice {
    /**
     * Approach 1: Brute Force
     * Complexity Analysis

     Time complexity : O(n^2)O(n
     2
     ). For each element, we try to find its complement by looping through the rest of array which takes O(n)O(n) time. Therefore, the time complexity is O(n^2)O(n
     2
     ).

     Space complexity : O(1)O(1).
     */
    public static int[] twoSum(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[j] == target - nums[i]) {
                    return new int[] { i, j };
                }
            }
        }
        throw new IllegalArgumentException("No two sum solution");
    }

    /**
     * Complexity Analysis:
     Time complexity : O(n)O(n). We traverse the list containing nn elements exactly twice. Since the hash table reduces the look up time to O(1)O(1), the time complexity is O(n)O(n).
     Space complexity : O(n)O(n). The extra space required depends on the number of items stored in the hash table, which stores exactly nn elements.
     * @param nums
     * @param target
     * @return
     */
    public static int[] twoSumTwoPassHashTable(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], i);
        }
        for (int i = 0; i < nums.length; i++) {
            int complement = target - nums[i];
            if (map.containsKey(complement) && map.get(complement) != i) {
                return new int[] { i, map.get(complement) };
            }
        }
        throw new IllegalArgumentException("No two sum solution");
    }

    /**
     * Complexity Analysis:

     Time complexity : O(n)O(n). We traverse the list containing nn elements only once. Each look up in the table costs only O(1)O(1) time.

     Space complexity : O(n)O(n). The extra space required depends on the number of items stored in the hash table, which stores at most nn elements
     * @param nums
     * @param target
     * @return
     */
    public static int[] twoSumOnePassHashTable(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int complement = target - nums[i];
            if (map.containsKey(complement)) {
                return new int[] { map.get(complement), i };
            }
            map.put(nums[i], i);
        }
        throw new IllegalArgumentException("No two sum solution");
    }

    public static void main(String[] args) {
        int[] nums = {2, 7, 11, 15};
        int target = 9;
        printArr(twoSum(nums, target));
        printArr(twoSumTwoPassHashTable(nums, target));
        printArr(twoSumOnePassHashTable(nums, target));
    }

    private static void printArr(int[] ints) {
        for(int i : ints)
            System.out.print(i+",");
        System.out.println();
    }
}
